import { ChangeDetectorRef, Component, OnInit, ViewChild } from '@angular/core';
import { AfMessageService, AfNavigationService } from '@core';
import { STColumn, STColumnTag, STColumnBadge, STComponent } from '@delon/abc/st';
import { ModalHelper, _HttpClient } from '@delon/theme';

import { SecurityService } from '../security.service';

@Component({
  selector: 'app-security-role',
  templateUrl: './role.component.html'
})
export class SecurityRoleComponent implements OnInit {
  rows: any[] = [];
  queryParams: any = { roleName: '' };
  url = '';
  req = { params: this.queryParams, lazyLoad: false };

  isQuery = false;

  @ViewChild('st') private readonly st!: STComponent;
  columns: STColumn[] = [
    { title: '角色名称', index: 'name' },
    { title: '角色描述', index: 'descr' },
    { title: '角色', index: 'role' },
    { title: '更新人', index: 'lastModifiedBy' },
    { title: '更新日期', type: 'date', index: 'lastModifiedDate' },
    {
      title: '操作',
      buttons: [
        {
          text: '编辑',
          icon: 'edit',
          tooltip: `修改账号信息`,
          click: record => {
            this.editNavigate(record, 0);
          }
        },
        {
          text: '分配菜单',
          icon: 'safety',
          tooltip: `分配菜单`,
          click: record => {
            this.editNavigate(record, 1);
          }
        },
        {
          text: '更多',
          children: [
            {
              text: '删除角色',
              icon: 'delete',
              pop: {
                title: '您确定要删除角色吗？',
                okType: 'danger',
                icon: 'star'
              },
              tooltip: `删除角色，需先删除已分配菜单`,
              click: record => {
                this.deleteRole(record);
              }
            },
            {
              text: '删除菜单',
              icon: 'delete',
              pop: {
                title: '您确定要删除菜单吗？',
                okType: 'danger',
                icon: 'star'
              },
              tooltip: `删除角色，需先删除已分配菜单`,
              click: record => {
                this.deleteRoleMenu(record);
              }
            }
          ]
        }
      ]
    }
  ];

  constructor(
    private http: _HttpClient,
    private modal: ModalHelper,
    private cdr: ChangeDetectorRef,
    private msgSrv: AfMessageService,
    private navSrv: AfNavigationService,
    private securitySrv: SecurityService
  ) {}

  ngOnInit(): void {
    this.url = this.securitySrv.queryRoleUrl();
  }

  addNavigate(): void {
    this.navSrv.navigateByUrl('/security/role/edit?btn=add');
  }

  editNavigate(record: any, index: any) {
    this.navSrv.navigateByUrl(`/security/role/edit?btn=edit&record=${JSON.stringify(record)}&index=${index}`);
  }

  query(event: any = undefined) {
    // if (!this.queryParams.templeteType || this.queryParams.templeteType === '') {
    //   this.msgSrv.msg_warning(`请选择模版类型`);
    //   return;
    // }
    this.st.reset(this.queryParams);
  }

  deleteRoleMenu(record: any) {
    this.securitySrv.deleteRoleMenu(record.id).subscribe(res => {
      if (res.status) {
        this.msgSrv.msg_success(`成功删除已分配的菜单`);
      } else {
        this.msgSrv.msg_error(res.msg);
      }
    });
  }
  deleteRole(record: any) {
    this.securitySrv.deleteRole(record.id).subscribe(res => {
      if (res.status) {
        this.msgSrv.msg_success(`成功删除角色`);
        this.st.reset(this.queryParams);
      } else {
        this.msgSrv.msg_error(res.msg);
      }
    });
  }
}
